package com.carpool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyStep2Activity extends Activity {
	final int PICK_CONTACT = 0;
	final int PASS_CONTACT = 1;

	private Button btnContact, btnAdd, btnNext;
	private ListView listView;
	private TextView txtCustomAddress;

	ArrayAdapter<String> adapter;
	ArrayList<String> addresses = new ArrayList<String>();

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.step2);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			addresses = bundle.getStringArrayList("addresses");
		}

		btnContact = (Button)findViewById(R.id.btnContact);
		btnContact.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(), MyCustomContactActivity.class);
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("addresses", addresses);
				intent.putExtras(bundle);
				startActivityForResult(intent, PICK_CONTACT);
			}
		});

		txtCustomAddress = (TextView)findViewById(R.id.txtCustomAddress);
		btnAdd = (Button)findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
				try {
					List<Address> address = geoCoder.getFromLocationName(txtCustomAddress.getText().toString(), 1);
					if (address.size() > 0) 
					{
						addresses.add(address.get(0).getAddressLine(0) + "\n" + address.get(0).getAddressLine(1));
						Intent intent = new Intent(getApplicationContext(), MyStep2Activity.class);
						Bundle bundle = new Bundle();
						bundle.putStringArrayList("addresses", addresses);
						intent.putExtras(bundle);
						startActivity(intent);
					}
					else {
						Toast.makeText(getBaseContext(), "No address could be found.", Toast.LENGTH_LONG).show();
					}
				} catch (IOException e) {          
				} catch (NullPointerException e) {
				};
			}
		});

		btnNext = (Button)findViewById(R.id.btnNext);
		btnNext.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(), MyStep3Activity.class);
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("addresses", addresses);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
		});

		listView = (ListView)findViewById(R.id.listView);
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, addresses);
		listView.setTextFilterEnabled(true);
		OnItemLongClickListener listener = new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) {
				AlertDialog.Builder adb = new AlertDialog.Builder(MyStep2Activity.this);
				adb.setTitle("Delete item?");
				adb.setMessage("Confirmation deletion of:\n" + listView.getItemAtPosition(position));
				final int positionToRemove = position;
				if (positionToRemove == 0) {
					Toast.makeText(getBaseContext(), "Cannot delete start point.", Toast.LENGTH_LONG).show();
				}
				else {
					adb.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							addresses.remove(positionToRemove);
							adapter.notifyDataSetChanged();
						}});
					adb.setNegativeButton("Cancel", null);
					adb.show();
				}
				return true;
			}
		};
		listView.setOnItemLongClickListener(listener);
		listView.setAdapter(adapter);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
	super.onActivityResult(requestCode, resultCode, intent);
	Bundle extras = intent.getExtras();

	if (extras != null) {
		addresses.add(extras.getString("address"));
	}

	adapter.notifyDataSetChanged();

}

}
